<template>
	<div id="accountJD">
		<div id="header">
			<img @click="$router.go(-1)" src="../../static/img/return.png"/>
			<p>绑定京东账号</p>
		</div>
		<input type="text" placeholder="请填写京东账号" class="account_first" v-model="addList[0].account" />
		<div v-for="(item,key) in addList.slice(1)" class="add_item">
			<input type="text" placeholder="请填写京东账号" v-model="item.account" />
			<img @click="remove(key+1)" src="../../static/img/account-09.png"/>
		</div>
		<div @click="add" id="add">
			+新的京东账号
		</div>
		<input id="sub" @click="update" type="button" value="提交" />
	</div>
</template>

<script>
export default{
	name: 'accountJD',
	data:function(){
		return{
			addList:[
				{
					account: '',
				},
			],
		}
	},
	methods:{
		add:function(){
			this.addList.push({account: ''});
		},
		remove:function(key){
			this.addList.splice(key,1);
		},
		// 获取我的绑定账号
		getMyAccount: function(){
			var _this = this;
			let postData = _this.$qs.stringify({
				type : 2
			});
			_this.$axios.post("/user/bindAccount/getMyBindAccount", postData).then(function(res){
				var data = res.data;
				if(data.message == "succ"){
					if(data.result.length != 0){
						_this.addList = data.result;
					}
				}else{
					_this.$alert(data.result);
				}
			}).catch(function(err){
				console.log(err);
			});
		},
		// 修改绑定账号
		update: function(){
			var _this = this;
			_this.$loading();
			
			var account = [];
			$.each(_this.addList, function(i) {
				account.push(_this.addList[i].account);
			});
			let postData = _this.$qs.stringify({
				type : 2,
				account: JSON.stringify(account)
			});
			_this.$axios.post("/user/bindAccount/addOrUpdate", postData).then(function(res){
				var data = res.data;
				if(data.message == "succ"){
					$("#loading").remove();
					_this.$alert("修改成功");
					_this.$router.go(-1);
				}else{
					_this.$alert(data.result);
				}
			}).catch(function(err){
				console.log(err);
			});
		}
	},
	created: function(){
		this.getMyAccount();
	}
}
</script>

<style scoped>
/* ========== */
/* = Header = */
/* ========== */
#header{
	width: 100%;
	background-color: #fff;
	position: fixed;
	top: 0;
	left: 0;
	height: 4rem;
	display: flex;
	align-items: center;
	justify-content: center;
	border-bottom: 1px solid #EEEEEE;
	font-size: 1.6rem;
	color: #4F4F4F;
	box-sizing: border-box;
	z-index: 10;
}
#header img{
	height: 2rem;
	display: block;
	position: absolute;
	top: 1rem;
	left: 2rem;
}
#accountJD{
	min-height: 100%;
	padding-top: 5rem;
	padding-bottom: 0.5rem;
	box-sizing: border-box;
	background-color: #fff;
}
.account_first{
	width: 94%;
	font-size: 1.2rem;
	height: 3.4rem;
	margin: 0 auto;
	display: block;
	border: 1px solid #EEEEEE;
	padding-left: 1rem;
	box-sizing: border-box;
}
.add_item{
	display: flex;
	width: 94%;
	height: 3.4rem;
	margin: 0 auto;
	padding-left: 1rem;
	box-sizing: border-box;
	border: 1px solid #EEEEEE;
	margin-top: 1rem;
	font-size: 1.2rem;
}
.add_item input{
	height: 100%;
	width: 100%;
	border: none;
	background-color: transparent;
}
.add_item img{
	height: 100%;
	display: block;
}
#add{
	width: 94%;
	margin: 0 auto;
	border: 1px solid #FF6565;
	color: #FF6565;
	background-color: #fff;
	height: 3.4rem;
	display: flex;
	align-items: center;
	justify-content: center;
	box-sizing: border-box;
	margin-top: 1.5rem;
	font-size: 1.2rem;
}
#sub{
	width: 94%;
	display: block;
	margin: 0 auto;
	border: none;
	color: #FFFFFF;
	background-color: #FF6565;
	height: 3.4rem;
	box-sizing: border-box;
	margin-top: 1rem;
	font-size: 1.2rem;
}

</style>